<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>redis发布者订阅者 | 欢迎来到guo的博客!</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="什么是redis发布者订阅者？redis发布订阅(pub&#x2F;sub)是一种消息通信模式，包含发送者(pub)来发送消息，与订阅者(sub)来接收消息。订阅者可以订阅一个或若多个频道（channel），而发布者可以向指定的频道发送消息，所有订阅此频道的订阅者都会收到此消息。像是我们在网络上对哪个博主的文章感兴趣就可以来订阅它，来获取它的消息的推送，不感兴趣后可以取消订阅取消获取此博主的消息推送。你可以">
<meta property="og:type" content="article">
<meta property="og:title" content="redis发布者订阅者">
<meta property="og:url" content="http://shangguan-lingfeng.gitee.io/blog1/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/index.html">
<meta property="og:site_name" content="欢迎来到guo的博客!">
<meta property="og:description" content="什么是redis发布者订阅者？redis发布订阅(pub&#x2F;sub)是一种消息通信模式，包含发送者(pub)来发送消息，与订阅者(sub)来接收消息。订阅者可以订阅一个或若多个频道（channel），而发布者可以向指定的频道发送消息，所有订阅此频道的订阅者都会收到此消息。像是我们在网络上对哪个博主的文章感兴趣就可以来订阅它，来获取它的消息的推送，不感兴趣后可以取消订阅取消获取此博主的消息推送。你可以">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-02-01T07:24:03.010Z">
<meta property="article:modified_time" content="2021-04-02T03:43:40.156Z">
<meta property="article:author" content="Jerry">
<meta property="article:tag" content="redis">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/blog1/atom.xml" title="欢迎来到guo的博客!" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/blog1/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/blog1/css/style.css">

  
    
<link rel="stylesheet" href="/blog1/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog1/" id="logo">欢迎来到guo的博客!</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/blog1/">Home</a>
        
          <a class="main-nav-link" href="/blog1/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/blog1/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Suche"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Suche"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://shangguan-lingfeng.gitee.io/blog1"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-redis发布者订阅者" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/" class="article-date">
  <time class="dt-published" datetime="2021-02-01T07:24:03.010Z" itemprop="datePublished">2021-02-01</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      redis发布者订阅者
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="什么是redis发布者订阅者？"><a href="#什么是redis发布者订阅者？" class="headerlink" title="什么是redis发布者订阅者？"></a>什么是redis发布者订阅者？</h2><p>redis发布订阅(pub/sub)是一种消息通信模式，包含发送者(pub)来发送消息，与订阅者(sub)来接收消息。<br>订阅者可以订阅一个或若多个频道（channel），而发布者可以向指定的频道发送消息，<br>所有订阅此频道的订阅者都会收到此消息。像是我们在网络上对哪个博主的文章感兴趣就可以来订阅它，来获取它的<br>消息的推送，不感兴趣后可以取消订阅取消获取此博主的消息推送。你可以关注的博主有很多。</p>
<p>##常用订阅者模块命令<br>subscribe： 订阅一个或者多个频道<br>unsubscribe: 退订一个或者多个频道<br>publish: 向通道发送消息<br>psubscribe: 订阅给定模式相匹配的所有频道<br>punsubscribe: 退订 给定模式所有的频道，若未指定模式，退订所有频道</p>
<h2 id="实际演示Redis发布者订阅者"><a href="#实际演示Redis发布者订阅者" class="headerlink" title="实际演示Redis发布者订阅者"></a>实际演示Redis发布者订阅者</h2><p>首先我们需要开启两个Redis客户端充当发布者和订阅者,在第一个客户端创建一个订阅频道YouTube</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; subscribe YouTube</span><br><span class="line">Reading messages... (press Ctrl-C to quit)</span><br><span class="line">1) &quot;subscribe&quot;</span><br><span class="line">2) &quot;YouTube&quot;</span><br><span class="line">3) (integer) 1</span><br></pre></td></tr></table></figure>
<p>继续创建发布者客户端在订阅者客户端创建的YouTube频道发布消息，使订阅者客户端能够收到消息。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; publish YouTube &quot;发送的第一条消息&quot;</span><br><span class="line">(integer) 1     #客户端订阅数量</span><br></pre></td></tr></table></figure>
<p>订阅者的客户端会收到如下消息</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">1) &quot;message&quot;    #表示接收到的消息</span><br><span class="line">2) &quot;YouTube&quot;    #创建的频道名</span><br><span class="line">3) &quot;发送的第一条消息&quot;   #接收到发布者发送的消息</span><br></pre></td></tr></table></figure>
<h2 id="redis发布订阅命令返回信息"><a href="#redis发布订阅命令返回信息" class="headerlink" title="redis发布订阅命令返回信息"></a>redis发布订阅命令返回信息</h2><p>订阅者客户端可能收到三种类型的回复。每种类型的回复都包含3个值，通常第一个值是消息的类型，根据消息类型的不同，第二、三个值的含义也不同。以下是消息类型可能的取值：<br><strong>subscribe</strong>：表示订阅成功。第二个值是订阅成功的频道名称，第三个值是当前客户端订阅的频道数量。<br><strong>message</strong>：表示接收到的消息。第二个值表示产生 消息的频道名称，第三个值是消息的内容。<br><strong>unsubscribe</strong>：表示成功取消订阅成功。第二个值是取消订阅成功的频道名称，第三个值是当前客户端订阅的频道数量，当此值为0时客户端会退出订阅状态。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/" data-id="cknublgbv000uv8uhdfdk880h" data-title="redis发布者订阅者" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/redis/" rel="tag">redis</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog1/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Neuer</strong>
      <div class="article-nav-title">
        
          redis键空间事件
        
      </div>
    </a>
  
  
    <a href="/blog1/2021/01/31/axios%E5%B0%81%E8%A3%85/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Älter</strong>
      <div class="article-nav-title">axios封装</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/CentOS/" rel="tag">CentOS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/Flask/" rel="tag">Flask</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/django/" rel="tag">django</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/docker/" rel="tag">docker</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/gitee/" rel="tag">gitee</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/linux/" rel="tag">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/mysql/" rel="tag">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/python/" rel="tag">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" rel="tag">python基础知识</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/vue/" rel="tag">vue</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/webshell/" rel="tag">webshell</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/%E4%BA%91%E4%B8%BB%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" rel="tag">云主机实现视频直播</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/%E5%85%B3%E4%BA%8E%E7%AC%AC%E4%B8%89%E6%96%B9/" rel="tag">关于第三方</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/blog1/tags/CentOS/" style="font-size: 17.5px;">CentOS</a> <a href="/blog1/tags/Flask/" style="font-size: 10px;">Flask</a> <a href="/blog1/tags/django/" style="font-size: 12.5px;">django</a> <a href="/blog1/tags/docker/" style="font-size: 12.5px;">docker</a> <a href="/blog1/tags/gitee/" style="font-size: 10px;">gitee</a> <a href="/blog1/tags/linux/" style="font-size: 10px;">linux</a> <a href="/blog1/tags/mysql/" style="font-size: 12.5px;">mysql</a> <a href="/blog1/tags/python/" style="font-size: 15px;">python</a> <a href="/blog1/tags/python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" style="font-size: 17.5px;">python基础知识</a> <a href="/blog1/tags/redis/" style="font-size: 12.5px;">redis</a> <a href="/blog1/tags/vue/" style="font-size: 20px;">vue</a> <a href="/blog1/tags/webshell/" style="font-size: 10px;">webshell</a> <a href="/blog1/tags/%E4%BA%91%E4%B8%BB%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" style="font-size: 10px;">云主机实现视频直播</a> <a href="/blog1/tags/%E5%85%B3%E4%BA%8E%E7%AC%AC%E4%B8%89%E6%96%B9/" style="font-size: 12.5px;">关于第三方</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/02/">二月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/01/">一月 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog1/2021/05/14/docker+CentOS+mysql%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5/">docker+CentOS+mysql实现主从同步</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/13/mysql%E5%AE%9E%E7%8E%B0%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB(%E5%9C%A8%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5%E5%9F%BA%E7%A1%80%E4%B8%8A)/">mysql实现读写分离</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/12/%E5%9C%A8%E9%A1%B9%E7%9B%AE%E4%B8%AD%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3/">在项目中自动生成接口文档</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/12/mysql%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5/">mysql实现主从同步</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/09/CentOS%E9%83%A8%E7%BD%B2flask%E9%A1%B9%E7%9B%AE/">nginx部署flask项目</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 Jerry<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/blog1/" class="mobile-nav-link">Home</a>
  
    <a href="/blog1/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/blog1/js/jquery-3.4.1.min.js"></script>



  
<script src="/blog1/fancybox/jquery.fancybox.min.js"></script>




<script src="/blog1/js/script.js"></script>





  </div>
</body>
</html>